package com.mathdroid.gui.main;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.ToolTipManager;

import com.mathdroid.core.Config;
import com.mathdroid.gui.CenteredFrame;
import com.mathdroid.gui.main.abilities.AbilityPanel;
import com.mathdroid.gui.main.advertising.Advertising;
import com.mathdroid.gui.main.browser.BrowserPanel;
import com.mathdroid.gui.main.buffs.BuffsPanel;
import com.mathdroid.gui.main.character.CharacterPanel;
import com.mathdroid.gui.main.encounter.EncounterPanel;
import com.mathdroid.gui.main.filter.FilterPanel;
import com.mathdroid.gui.main.talents.TalentButton;
import com.mathdroid.gui.main.talents.TalentsPanel;
import com.mathdroid.gui.menu.MenuBar;
import com.mathdroid.model.EModelObserverEvent;
import com.mathdroid.model.IModelObserver;
import com.mathdroid.model.Model;

/**
 * This is the main window of the application.
 */
public class DroidFrame extends CenteredFrame implements IModelObserver {
	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Contains all left tabs.
	 */
	private JTabbedPane left = new JTabbedPane();
	
	/**
	 * Contains all right tabs.
	 */
	private JTabbedPane rightTop = new JTabbedPane();
	
	/**
	 * The advertising panel.
	 */
	private Advertising rightBottom = new Advertising();
	
	/**
	 * The character configuration panel.
	 */
	private CharacterPanel characterPanel = new CharacterPanel();
	
	/**
	 * The talents configuration panel.
	 */
	private TalentsPanel talentsPanel = new TalentsPanel();
	
	/**
	 * The buff configuration panel.
	 */
	private BuffsPanel buffsPanel = new BuffsPanel();
	
	/**
	 * The ability configuration panel.
	 */
	private AbilityPanel abilityPanel = new AbilityPanel();
	
	/**
	 * The encounter configuration panel.
	 */
	private EncounterPanel encounterPanel = new EncounterPanel();
	
	/**
	 * The item browser.
	 */
	private BrowserPanel browserPanel = new BrowserPanel();
	
	/**
	 * The item filter panel.
	 */
	private FilterPanel filterPanel = new FilterPanel();
	
	/**
	 * The item editor.
	 */
	private ItemEditorPanel itemEditorPanel = new ItemEditorPanel();
	
	/**
	 * The menu bar.
	 */
	private MenuBar menuBar = new MenuBar();
	
	/**
	 * Builds the main window. Do not forget to set visible.
	 */
	public DroidFrame() {
		super("Math-Droid-Z42 (" + Config.getProperty("version") + ")");
		JSplitPane rightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
				this.rightTop, this.rightBottom.getPanel());
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 
				this.left, rightSplitPane);

		this.rightTop.setPreferredSize(new Dimension(0, 0));
		
		splitPane.setResizeWeight(0);
		rightSplitPane.setResizeWeight(1);
		this.setContentPane(splitPane);
		Model.registerObserver(this);
		
		ToolTipManager tm = ToolTipManager.sharedInstance();
		tm.setInitialDelay(0);
		
		this.addTabs();
		
		this.setJMenuBar(this.menuBar);
		
		final Component parent = this;
		this.addWindowListener(new WindowListener() {
			@Override public void windowActivated(final WindowEvent arg0) { }
			@Override public void windowClosed(final WindowEvent arg0) { }

			@Override
			public void windowClosing(final WindowEvent arg0) {
				int n = JOptionPane.showConfirmDialog(
						parent, 
					    "Exit without saving?",
					    "Exit",
					    JOptionPane.YES_NO_OPTION);
				if (n == 0) {
					dispose();
				}
			}

			@Override public void windowDeactivated(final WindowEvent arg0) { }
			@Override public void windowDeiconified(final WindowEvent arg0) { }
			@Override public void windowIconified(final WindowEvent arg0) { }
			@Override public void windowOpened(final WindowEvent arg0) { }
		});
		
		this.setIconImage(new ImageIcon(
				TalentButton.class.getClass().getResource(
				"/com/mathdroid/gui/images/frameicon.png")).getImage());
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.pack();
		
		this.rightBottom.execute();
	}

	/**
	 * Add the tabs to the left and right JTabbedPane.
	 */
	private void addTabs() {
		this.left.addTab("Character", null, this.characterPanel, 
				"Configure the equipped items and show character statistics.");
		
		this.left.addTab("Advanced Class Skills", null, this.talentsPanel, 
				"Configure the advanced class skill trees.");
		
		this.left.addTab("Buffs", null, this.buffsPanel, 
				"Configure the active buffs.");
		
		this.left.addTab("Abilities", null, this.abilityPanel, 
				"Configure the ability priorities.");
		
		this.left.addTab("Encounter", null, this.encounterPanel, 
				"Configure the encounter.");
		
		this.rightTop.addTab("Browser", null, this.browserPanel, 
				"Browse through available items and compare them.");
//		this.rightTop.addTab("Filter", null, this.filterPanel, 
//				"Select the items, that have to be shown in the browser.");
//		this.rightTop.addTab("Editor", null, this.itemEditorPanel, 
//				"Edit an item.");
	}

	@Override
	public final void modelChanged(final EModelObserverEvent e) {
		if (e == EModelObserverEvent.SPEC_LOADED 
				|| e == EModelObserverEvent.PROFILE_LOADED) {
			this.talentsPanel.modelChanged();
		}
	}
}
